/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mx.edu.itschapala.sistemas.septimo.vista;

import java.awt.event.KeyEvent;
import mx.edu.itschapala.sistemas.septimo.control.ClientelnLocal;
import mx.edu.itschapala.sistemas.septimo.control.ProductoLnLocal;
import mx.edu.itschapala.sistemas.septimo.modelo.Cliente;
import mx.edu.itschapala.sistemas.septimo.modelo.Producto;
import mx.edu.itschapala.sistemas.septimo.vista.enumeracion.TipoVista;
import static mx.edu.itschapala.sistemas.septimo.vista.enumeracion.TipoVista.VENTANA_REGISTRO;

/**
 *
 * @author Estudiante
 */
public class ClienteCrearEditar extends javax.swing.JFrame {

    /**
     * Creates new form ClienteCrearEditar
     */  private ClientelnLocal clienteControl;
    private TipoVista tipoVentana;
private Cliente cliente;
    public ClienteCrearEditar(Cliente cliente,ClientelnLocal clienteControl,TipoVista tipoVentana) {
          this.clienteControl = clienteControl;
    this.cliente=cliente;
        this.tipoVentana = tipoVentana;
        initComponents();
                  this.dispose();
        this.setResizable(false);
         switch (tipoVentana) {
            case VENTANA_REGISTRO:
                jLabel6.setText("NUEVO CLIENTE");
                break;
            case VENTANA_BUSQUEDA:
                jLabel6.setText("BUSCAR POR ID");
                jTextField2.setEditable(false);
                jTextField3.setEditable(false);
                jTextField4.setEditable(false);
                jTextField5.setEditable(false);
                jButton2.setVisible(false);
                break;
            case VENTANA_MODIFICAR:
                jLabel6.setText("MODIFICAR CLIENTE");
                jTextField1.setEditable(false);  
                jTextField2.setText(cliente.getNombre());
                jTextField1.setText(cliente.getId()+"");
                jTextField3.setText(cliente.getTelefono()+"");
                jTextField4.setText(cliente.getDireccion());
                jTextField5.setText(cliente.getSexo());
                break;
            case VENTANA_ELIMINAR:
                  jLabel6.setText("ELIMINAR CLIENTE");
                    jTextField1.setText(cliente.getId()+"");
                jTextField2.setEditable(false);
                jTextField3.setEditable(false);
                jTextField4.setEditable(false);
                jTextField5.setEditable(false);
                break;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel6 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(255, 0, 0));
        jLabel6.setText("Cliente");

        jButton1.setText("Aceptar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jPanel1.setLayout(new java.awt.GridLayout(5, 0));

        jLabel1.setText("id");
        jPanel1.add(jLabel1);

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField1KeyPressed(evt);
            }
        });
        jPanel1.add(jTextField1);

        jLabel2.setText("nombre");
        jPanel1.add(jLabel2);
        jPanel1.add(jTextField2);

        jLabel5.setText("telefono");
        jPanel1.add(jLabel5);

        jTextField3.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField3KeyPressed(evt);
            }
        });
        jPanel1.add(jTextField3);

        jLabel4.setText("direccion");
        jPanel1.add(jLabel4);
        jPanel1.add(jTextField4);

        jLabel3.setText("sexo");
        jPanel1.add(jLabel3);
        jPanel1.add(jTextField5);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 341, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(86, 86, 86)
                        .addComponent(jButton1)
                        .addGap(60, 60, 60)
                        .addComponent(jButton2))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(149, 149, 149)
                        .addComponent(jLabel6)))
                .addContainerGap(37, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 88, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addComponent(jButton2)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        switch (tipoVentana) {
            case VENTANA_REGISTRO:

            Cliente c = new Cliente();
            c.setNombre(jTextField2.getText());
            c.setId(Integer.parseInt(jTextField1.getText()));
            c.setTelefono(Integer.parseInt(jTextField3.getText()));
            c.setDireccion(jTextField4.getText());
            c.setSexo(jTextField5.getText());
            clienteControl.registrar(c);
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            break;
            case VENTANA_BUSQUEDA:
            this.dispose();
            break;
            case VENTANA_MODIFICAR:

            c = new Cliente();
            c.setNombre(jTextField2.getText());
            c.setId(Integer.parseInt(jTextField1.getText()));
            c.setTelefono(Integer.parseInt(jTextField3.getText()));
            c.setDireccion(jTextField4.getText());
            c.setSexo(jTextField5.getText());
           clienteControl.modificar(c);
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField2.setEditable(false);
            jTextField3.setEditable(false);
            jTextField4.setEditable(false);
            jTextField5.setEditable(false);
            break;
            case VENTANA_ELIMINAR:
            c = new Cliente();
            c.setId(Integer.parseInt(jTextField1.getText()));
           clienteControl.eliminar(c);
                jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField2.setEditable(false);
            jTextField3.setEditable(false);
            jTextField4.setEditable(false);
            jTextField5.setEditable(false);
                break;

        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyPressed
        if (tipoVentana != TipoVista.VENTANA_REGISTRO) {
            if (evt.getKeyCode() == KeyEvent.VK_ENTER) {

                Cliente c = clienteControl.buscar(Integer.parseInt(jTextField1.getText()));
                if (c != null) {
                    jTextField2.setText(c.getNombre());
                    jTextField3.setText(String.valueOf(c.getTelefono()));
                    jTextField4.setText(c.getDireccion());
                    jTextField5.setText(c.getSexo());
                    if (tipoVentana == TipoVista.VENTANA_MODIFICAR) {
                        jTextField2.setEditable(true);
                        jTextField3.setEditable(true);
                        jTextField4.setEditable(true);
                        jTextField5.setEditable(true);

                    }
                }
            }
        }
    }//GEN-LAST:event_jTextField1KeyPressed

    private void jTextField3KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField3KeyPressed

    }//GEN-LAST:event_jTextField3KeyPressed

    /**
     * @param args the command line arguments
     */
  

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    // End of variables declaration//GEN-END:variables
}
